package org.groupg.project;

import org.apache.commons.collections4.BidiMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.OrderedMap;
import org.apache.commons.collections4.bidimap.TreeBidiMap;
import org.apache.commons.collections4.list.LazyList;
import org.apache.commons.collections4.map.LRUMap;
import org.apache.commons.collections4.map.LinkedMap;
import org.apache.commons.collections4.map.MultiKeyMap;
import org.apache.commons.collections4.map.MultiValueMap;

import java.util.*;

public class CommonsCollectionsDemo {

    public static void main(String[] args) {
        // 1. MultiValueMap（多值映射）
        MultiValueMap<String, String> departmentMap = new MultiValueMap<>();
        departmentMap.put("Engineering", "Alice");
        departmentMap.put("Engineering", "Bob");
        departmentMap.put("Marketing", "Charlie");
        departmentMap.putAll("HR", Arrays.asList("David", "Eva"));

        System.out.println("部门结构:");
        for (String dept : departmentMap.keySet()) {
            System.out.println(dept + ": " + departmentMap.get(dept));
        }

        // 2. BidiMap（双向映射）
        BidiMap<String, Integer> countryCodes = new TreeBidiMap<>();
        countryCodes.put("United States", 1);
        countryCodes.put("United Kingdom", 44);

        countryCodes.put("Japan", 81);

        System.out.println("\n国家代码:");
        System.out.println("美国代码: " + countryCodes.get("United States"));
        countryCodes.put("United Kingdom1", 1);// 由于添加的内容 United Kingdom1 覆盖了原本的配置，导致内存中已经覆盖，不能处理。
        System.out.println("美国代码: " + countryCodes.get("United States"));
        System.out.println("代码44的国家: " + countryCodes.getKey(44));

        // 3. LRUMap（最近最少使用缓存）
        LRUMap<String, String> cache = new LRUMap<>(3);
        cache.put("key1", "value1");
        cache.put("key2", "value2");
        cache.put("key3", "value3");

        System.out.println("\n初始缓存: " + cache);

        cache.get("key1"); // 访问key1使其保留
        cache.put("key4", "value4"); // 超出容量，淘汰key2

        System.out.println("添加key4后: " + cache);

        // 4. 集合工具类
        List<String> list1 = Arrays.asList("A", "B", "C", "D");
        List<String> list2 = Arrays.asList("C", "D", "E", "F");

        System.out.println("\n集合操作:");
        System.out.println("并集: " + CollectionUtils.union(list1, list2));
        System.out.println("交集: " + CollectionUtils.intersection(list1, list2));
        System.out.println("差集: " + CollectionUtils.subtract(list1, list2));

        // 5. 转换和过滤
        Collection<String> filtered = CollectionUtils.select(list1,
                item -> item.compareTo("C") > 0);
        System.out.println("过滤大于C的元素: " + filtered);

        // 6. 有序Map
        OrderedMap<String, Integer> orderedMap = new LinkedMap<>();
        orderedMap.put("First", 1);
        orderedMap.put("Second", 2);
        orderedMap.put("Third", 3);

        System.out.println("\n有序Map:");
        System.out.println("第一个键: " + orderedMap.firstKey());
        System.out.println("下一个键: " + orderedMap.nextKey("First"));

        // 7. 多键Map
        MultiKeyMap<Object, String> multiKeyMap = new MultiKeyMap<>();
        multiKeyMap.put("John", "Doe", "Employee");
        multiKeyMap.put("Jane", "Smith", "Manager");

        System.out.println("\n多键Map:");
        System.out.println("John Doe: " + multiKeyMap.get("John", "Doe"));

        // 8. 惰性集合
        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
        List<Integer> lazyList = LazyList.lazyList(new ArrayList<>(),
                () -> (int) (Math.random() * 100));

        for (int i = 0; i < numbers.size(); i++) {
            lazyList.get(i); // 访问时生成
        }

        System.out.println("惰性生成的列表: " + lazyList);

        // 9. 迭代器工具
        List<String> combined = new ArrayList<>();
        Iterator<String> it1 = list1.iterator();
        Iterator<String> it2 = list2.iterator();

        Iterator<String> chainedIt = IteratorUtils.chainedIterator(it1, it2);
        while (chainedIt.hasNext()) {
            combined.add(chainedIt.next());
        }

        System.out.println("链式迭代结果: " + combined);
    }
}